package com.web.comment.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.util.CommonUtil;
import com.web.comment.po.CommentPo;
import com.web.comment.service.CommentService;

@Service("commentService")
@Transactional(rollbackFor = { Exception.class })
public class CommentDAO implements CommentService{

	@Resource 
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<CommentPo> getCommentList(String queryString, int first,
			int pageSize) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		 session = this.sessionFactory.getCurrentSession();
      		 query = session.createQuery(queryString);
             query.setFirstResult(first);
             query.setMaxResults(pageSize);
             List<CommentPo> comments = query.list(); 
             return comments;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
		} 
		return null;
	}

	public int total(String queryString) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		 session = this.sessionFactory.getCurrentSession();
      		 StringBuffer sqlBuffer = new StringBuffer();
      		 sqlBuffer.append("From CommentPo as c ");
      		 sqlBuffer.append(" left outer join fetch c.user as u");
      		 sqlBuffer.append(" left outer join fetch c.hotel as h");
      		 sqlBuffer.append(" where 1=1 ");
      		 sqlBuffer.append(queryString);
      		 query = session.createQuery(sqlBuffer.toString());  
      		 return query.list().size() ;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
		} 
		return 0;
	}

	public int deleteComments(String commentIds) {
		// TODO Auto-generated method stub
		Session session = null;
    	Query query = null;
      	try {
      		 
      		 if(CommonUtil.isNotNull(commentIds)){
      			String sql = "delete from CommentPo where commentId in (" + commentIds + ")";
      			session = this.sessionFactory.getCurrentSession();
         		query = session.createQuery(sql);
         		int count = query.executeUpdate();
                return count;
      		 } else{
      			return -1; 
      		 }
      		 
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
      		throw new RuntimeException();
		} 
	}

	public CommentPo getCommentByCommentId(Integer commentId) {
		// TODO Auto-generated method stub
		Session session = null;
		Query query = null;
      	try {
      		session = this.sessionFactory.getCurrentSession();
  			StringBuffer hql = new StringBuffer();
    		hql.append("Select a From CommentPo a");
    		hql.append(" left outer join fetch a.user");
    		hql.append(" left outer join fetch a.hotel");
    		hql.append(" where a.commentId = '");
    		hql.append(commentId);
    		hql.append("'");
            query = session.createQuery(hql.toString());
            CommentPo comment = (CommentPo)query.list().get(0);
            return comment;
      	} catch (Exception e) {
			// TODO: handle exception
      		e.printStackTrace();
		} 
		return null;
	}

}
